//
//  MyTabInfoViewTableView.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2020/9/10.
//  Copyright © 2020 moon. All rights reserved.
//

#import "MyTabInfoViewTableView.h"
#import "MenuTableViewCell.h"
@implementation MyTabInfoViewTableView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.tabBarItem.title = @"Table";
        [self.showMoreButton addTarget:self action:@selector(__moreButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
        //
        self.tableView = [[MKUITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        self.tableView.bounces = NO;
        [self.contentView addSubview:self.tableView];
        self.flowlayout.items = @[self.showMoreButton,self.tableView];
    }
    return self;
}
- (void)layoutSubviews{
    CGRect oldBounds = self.tableView.bounds;
    [super layoutSubviews];
    if(!CGSizeEqualToSize(oldBounds.size, self.tableView.bounds.size)){
        [self.tableView.model reloadTableViewData];
    }
}
- (void)__moreButtonDidTap:(id)sender{
    self.cellModel[@"table_showMore"] = @(![self.cellModel mk_boolForKeyPath:@"table_showMore" otherwise:NO]);
    [self.cellModel refreshWithAnimated:YES];
}
- (void)reloadData{
    [super reloadData];
    self.showMoreButton.selected = [self.cellModel mk_boolForKeyPath:@"table_showMore" otherwise:NO];
    [self __reloadTableData];
}
- (void)__reloadTableData{
    [self.tableView.model removeAllSectionModels];
    MyMenuInfo *info = self.cellModel.modelValue;
    NSArray<Menu *> *histMenus = info.histMenus;
    if(![self.cellModel mk_boolForKeyPath:@"table_showMore" otherwise:NO]){
        histMenus = [histMenus subarrayWithRange:NSMakeRange(0, 2)];
    }
    for (Menu *menu in histMenus) {
        MKUITableViewCellModel *cm = [MKUITableViewCellModel modelWithValue:menu cellClass:[MenuTableViewCell class]];
        [self.tableView.model addCellModel:cm];
    }
    [self.tableView.model reloadTableViewData];
}
@end
